<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog"
    :title="isAdd ? '新增' : '编辑'" width="800px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="140px">

      <el-form-item label="	项目名称" prop="itemName">
        <el-input v-model="form.itemName" style="width: 300px;" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="	项目logo图" prop="itemImg">
        <single-pic v-model="form.itemImg" type="image" :num="1" :width="150" :height="150" />
      </el-form-item>
      <el-form-item label="技师" prop="technicianId">
        <el-select v-model="form.technicianId" clearable placeholder="请选择技师"  :remote-method="remoteMethod"  :loading="loading"
           filterable remote reserve-keyword>
            <el-option
              v-for="item in options"
              :key="item.uid"
              :label="item.technicianName"
              :value="item.uid">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="	原价" prop="originalPrice">
        <el-input-number min="0"	 v-model="form.originalPrice" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	折扣价" prop="discountedPrice">
        <el-input-number min="0"	 v-model="form.discountedPrice" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="项目服务时长(分钟)" prop="itemDate">
        <el-input-number min="0"	 v-model="form.itemDate" :min="0"></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addItem,editItem,initData  } from '@/api/technician'
import singlePic from '@/components/singlematerial'

export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  components:{
    singlePic
  },
  data() {
    return {
      defaultStartDate: new Date(2023, 1, 1),
      loading: false, dialog: false,
      form: {
        discountedPrice:0,//	折扣价
        itemDate:0,//	项目服务时长
        itemImg:'',//	项目logo图
        itemName:'',	//项目名称
        originalPrice:'',//原价
        // state:1,
        technicianId:'',//技师ID
      },
      options:[],
      CouponList: [],
      rules: {
        discountedPrice: [
          { required: true, message: '请输入项目折扣价', trigger: 'blur' },
        ],
        itemDate: [
          { required: true, message: '请输入项目服务时长', trigger: 'change' },
        ],
        itemImg: [
          { required: true, message: '请选择项目logo图', trigger: 'blur' },
        ],
        itemName: [
          { required: true, message: '请输入项目名称', trigger: 'change' },
        ],
        originalPrice: [
          { required: true, message: '请输入项目原价', trigger: 'change' },
        ],
        technicianId: [
          { required: true, message: '请选择技师', trigger: 'change' },
        ],

      }
    }
  },
  methods: {
    getTechnicianList(){

    },
    remoteMethod(query) {
        if (query !== '') {
          this.loading = true;

          let data={}
            data.page=0
            data.size=10
            data.technicianName=query
            initData(data).then(res=>{
              this.loading = false;
              this.options=res.content
            })

        } else {
          this.options = [];
        }
      },
    getGoods(p) {
      var ids = []
      p.forEach((item, index) => {
        ids.push(item.id)
      })
      this.form.productId = ids.join(",")
    },
    couponsType(e) {
      this.form = {
        discountedPrice:0,//	折扣价
        itemDate:0,//	项目服务时长
        itemImg:'',//	项目logo图
        itemName:'',	//项目名称
        originalPrice:'',//原价
        // state:1,
        technicianId:'',//技师ID
      }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true
          this.form.state=1
          addItem(this.form).then(res => {
            this.resetForm()
            this.$notify({
              title: '添加成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
            this.$parent.init()
          }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
          })
        }
      })
    },
    doEdit() {
      editItem(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      // this.$refs['form'].resetFields()
      this.form = {
        discountedPrice:0,//	折扣价
        itemDate:0,//	项目服务时长
        itemImg:'',//	项目logo图
        itemName:'',	//项目名称
        originalPrice:'',//原价
        // state:1,
        technicianId:'',//技师ID
      }
    }
  }
}
</script>

<style scoped></style>
